package com.ruoyi.web.controller.tlc.back;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.tlc.TlcFaq;
import com.ruoyi.system.service.tlc.ITlcFaqService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 参数配置 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/faq")
public class TlcFaqController extends BaseController
{
    @Autowired
    private ITlcFaqService tlcFaqService;

    @Log(title = "问答管理", businessType = BusinessType.LIST)
    @PreAuthorize("@ss.hasPermi('system:tlcfaq:list')")
    @GetMapping("/list")
    public TableDataInfo list(TlcFaq faq)
    {
        startPage();
        LambdaQueryWrapper<TlcFaq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderBy(true,false,TlcFaq::getCreateTime);
        if (StringUtils.isNotBlank(faq.getTitle())){
            queryWrapper.like(TlcFaq::getTitle, faq.getTitle());
        }
        if(faq.getShowFlag()!=null){
            queryWrapper.eq(TlcFaq::getShowFlag,faq.getShowFlag());
        }
        if(faq.getTop()!=null){
            queryWrapper.eq(TlcFaq::getTop,faq.getTop());
        }
        List<TlcFaq> list = tlcFaqService.list(queryWrapper);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:tlcfaq:add')")
    @Log(title = "问答管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TlcFaq faq)
    {
        faq.setCreateBy(getUsername());
        faq.setCreateTime(new Date());
        return toAjax(tlcFaqService.save(faq));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcfaq:update')")
    @Log(title = "问答管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody TlcFaq faq)
    {
        faq.setUpdateBy(getUsername());
        faq.setUpdateTime(new Date());
        return toAjax(tlcFaqService.updateById(faq));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcfaq:detail')")
    @GetMapping("/toUpdate/{id}")
    public AjaxResult toUpdate(@PathVariable Long id)
    {
        return AjaxResult.success(tlcFaqService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcfaq:show')")
    @Log(title = "问答管理", businessType = BusinessType.UPDATE)
    @PostMapping("/show")
    public AjaxResult show(@RequestBody Long[] ids)
    {
        LambdaUpdateWrapper<TlcFaq> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TlcFaq::getShowFlag,1).in(TlcFaq::getId, ids);
        return toAjax(tlcFaqService.update(new TlcFaq(), wrapper));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcfaq:hide')")
    @Log(title = "问答管理", businessType = BusinessType.UPDATE)
    @PostMapping("/hide")
    public AjaxResult hide(@RequestBody Long[] ids)
    {
        LambdaUpdateWrapper<TlcFaq> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(TlcFaq::getShowFlag,0).in(TlcFaq::getId, ids);
        return toAjax(tlcFaqService.update(new TlcFaq(), wrapper));
    }

    @PreAuthorize("@ss.hasPermi('system:tlcfaq:del')")
    @Log(title = "问答管理", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(@RequestBody Long[] ids)
    {
        return toAjax(tlcFaqService.removeBatchByIds(Arrays.asList(ids)));
    }
}
